package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.domain.MesBuyIn;
import com.ruoyi.system.domain.MesStock;
import com.ruoyi.system.mapper.MesStockMapper;
import com.ruoyi.system.service.IMesStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MesGetMatMapper;
import com.ruoyi.system.domain.MesGetMat;
import com.ruoyi.system.service.IMesGetMatService;

/**
 * 领料出库Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-01
 */
@Service
public class MesGetMatServiceImpl implements IMesGetMatService 
{
    @Autowired
    private MesGetMatMapper mesGetMatMapper;

    @Autowired
    private IMesStockService mesStockService;

    @Autowired
    private MesStockMapper mesStockMapper;
    /**
     * 查询领料出库
     * 
     * @param id 领料出库主键
     * @return 领料出库
     */
    @Override
    public MesGetMat selectMesGetMatById(Integer id)
    {
        return mesGetMatMapper.selectMesGetMatById(id);
    }

    /**
     * 查询领料出库列表
     * 
     * @param mesGetMat 领料出库
     * @return 领料出库
     */
    @Override
    public List<MesGetMat> selectMesGetMatList(MesGetMat mesGetMat)
    {
        return mesGetMatMapper.selectMesGetMatList(mesGetMat);
    }

    /**
     * 新增领料出库
     * 
     * @param mesGetMat 领料出库
     * @return 结果
     */
    @Override
    public int insertMesGetMat(MesGetMat mesGetMat)
    {
        List<MesGetMat> mtrs = mesGetMat.getMtrs();

        for (MesGetMat mtr : mtrs) {
            //1：处理数据
            mtr.setMatPlan(mtr.getMatId());
            mtr.setGetMatTime(mtr.getGetMatTime());
            mtr.setMatId(mtr.getMatId());
            mtr.setNeedMatNum(mtr.getNeedMatNum());
            mtr.setLocaId(mtr.getLocaId());
            //2.sql插入
            mesGetMatMapper.insertMesGetMat(mtr);

            //3.更新库存数量
            MesStock stock = mesStockMapper.selectByItemId(mtr.getMatId());
            if (stock != null){
                stock.setStockNum(mtr.getNeedMatNum() - stock.getStockNum());
                mesStockService.updateMesStock(stock);
            }
        }
        return 1;
        //return mesGetMatMapper.insertMesGetMat(mesGetMat);
    }

    /**
     * 修改领料出库
     * 
     * @param mesGetMat 领料出库
     * @return 结果
     */
    @Override
    public int updateMesGetMat(MesGetMat mesGetMat)
    {
        return mesGetMatMapper.updateMesGetMat(mesGetMat);
    }

    /**
     * 批量删除领料出库
     * 
     * @param ids 需要删除的领料出库主键
     * @return 结果
     */
    @Override
    public int deleteMesGetMatByIds(Integer[] ids)
    {
        return mesGetMatMapper.deleteMesGetMatByIds(ids);
    }

    /**
     * 删除领料出库信息
     * 
     * @param id 领料出库主键
     * @return 结果
     */
    @Override
    public int deleteMesGetMatById(Integer id)
    {
        return mesGetMatMapper.deleteMesGetMatById(id);
    }
}
